﻿using System;
using System.Linq;
using System.Web.Mvc;
using DataAccess.Entities;
using Zeenck.DataAccess;
using Zeenck.DataAccess.Repositories;
using Zeenck.Models;

namespace Zeenck.Controllers
{
    [Authorize]
    public class ProfileController : Controller
    {
        public ActionResult Index()
        {
            IRepository<Profile> repo = new ProfileRepository();
            IRepository<Account> accountRepo = new AccountRepository();
            Account account = accountRepo.GetAll().Where(x => x.UserName == User.Identity.Name).First();
            int profileId = repo.GetAll().ToList().Where(x => x.Account.Id == account.Id).Select(x => x.Id).First();
            Profile profile = ProfileModule.GetProfile(profileId);
            ZeenckProfile model = ProfileModule.ConvertToZeenckProfile(profile);
            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(int profileId)
        {
            Profile profile = ProfileModule.GetProfile(profileId);
            ZeenckProfile model = ProfileModule.ConvertToZeenckProfile(profile);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ZeenckProfile model)
        {
            return Redirect(Url.Profile(model.Id));
        }
    }
}
